package com.lyw.gulimall.ware.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyw.gulimall.ware.vo.MergePurchaseListVo;
import com.lyw.gulimall.ware.vo.PurchaseDoneVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lyw.gulimall.ware.entity.PurchaseEntity;
import com.lyw.gulimall.ware.service.PurchaseService;
import com.lyw.common.utils.PageUtils;
import com.lyw.common.utils.R;



/**
 * 采购信息
 *
 * @author liyawei
 * @email liyawei@gmail.com
 * @date 2022-05-31 12:20:13
 */
@RestController
@RequestMapping("ware/purchase")
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseService;

    // /ware/purchase/unreceive/list

    /**
     *  完成采购
     */
    @PostMapping("/done")
    public R userFinishedPurchase(@RequestBody PurchaseDoneVo purchaseDoneVo) {
        purchaseService.done(purchaseDoneVo);
        return R.ok();
    }

    /**
     *  员工领取采购单
     */
    @PostMapping("/received")
    public R userReceivePurchaseList(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                     @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit,
                                     @RequestBody List<Long> purchaseIds) {
        IPage<PurchaseEntity> list = purchaseService.getUserPurchaseLists(page, limit, purchaseIds);
        return R.ok().put("page", list);
    }

    /**
     * 查询未领取的采购单
     */
    @GetMapping("/unreceive/list")
    // @RequiresPermissions("ware:purchasedetail:list")
    public R getUnReceiveList(@RequestParam(value = "page",required = false, defaultValue = "1") Integer page,
                              @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit) {
        IPage<PurchaseEntity> list = purchaseService.queryUnReceivePurchaseListPage(page, limit);

        return R.ok().put("page", list);
    }

    /**
     * 进行采购需求合并
     */
    @PostMapping("/merge")
    // @RequiresPermissions("ware:purchasedetail:list")
    public R mergeList(@RequestBody MergePurchaseListVo mergePurchaseListVo) {
        purchaseService.mergePurchaseList(mergePurchaseListVo);

        return R.ok();
    }

    /**
     * 获取采购单信息
     */
    @RequestMapping("/list")
    // @RequiresPermissions("ware:purchase:list")
    public R list(@RequestParam(value = "page",required = false, defaultValue = "1") Integer page,
                  @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit,
                  @RequestParam(value = "key",required = false) String key,
                  @RequestParam(value = "status", required = false) Integer status){
        IPage<Map<String, Object>> pages = purchaseService.queryPageByCondition(page, limit, key, status);

        return R.ok().put("page", pages);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    // @RequiresPermissions("ware:purchase:info")
    public R info(@PathVariable("id") Long id){
		PurchaseEntity purchase = purchaseService.getById(id);

        return R.ok().put("purchase", purchase);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("ware:purchase:save")
    public R save(@RequestBody PurchaseEntity purchase){
        purchase.setCreateTime(new Date());
        purchase.setUpdateTime(new Date());

		purchaseService.save(purchase);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("ware:purchase:update")
    public R update(@RequestBody PurchaseEntity purchase){
        purchase.setUpdateTime(new Date());
		purchaseService.updateById(purchase);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("ware:purchase:delete")
    public R delete(@RequestBody Long[] ids){
		purchaseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
